home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / dskut / aspisrc.zip / GETDATE.Y < prev    next >
Text File  |  1992-01-26  |  22KB  |  897 lines

  1. %{
  2. /* $Revision: 2.1 $
  3. **
  4. **  Originally written by Steven M. Bellovin <smb@research.att.com> while
  5. **  at the University of North Carolina at Chapel Hill.  Later tweaked by
  6. **  a couple of people on Usenet.  Completely overhauled by Rich $alz
  7. **  <rsalz@bbn.com> and Jim Berets <jberets@bbn.com> in August, 1990;
  8. **  send any email to Rich.
  9. **
  10. **  This grammar has eight shift/reduce conflicts.
  11. **
  12. **  This code is in the public domain and has no copyright.
  13. */
  14. /* SUPPRESS 287 on yaccpar_sccsid *//* Unusd static variable */
  15. /* SUPPRESS 288 on yyerrlab *//* Label unused */
  16.  
  17. #ifdef __GNUC__
  18. #define alloca __builtin_alloca
  19. #else
  20. #ifdef sparc
  21. #include <alloca.h>
  22. #else
  23. #ifdef _AIX /* for Bison */
  24. #pragma alloca
  25. #else
  26. char *alloca ();
  27. #endif
  28. #endif
  29. #endif
  30.  
  31. #include <stdio.h>
  32. #include <ctype.h>
  33.  
  34. #if    defined(vms)
  35. #include <types.h>
  36. #include <time.h>
  37. #else
  38. #include <sys/types.h>
  39. #if    defined(USG)
  40. /*
  41. **  Uncomment the next line if you need to do a tzset() call to set the
  42. **  timezone, and don't have ftime().  Some SystemV releases, I think.
  43. */
  44. /*#define NEED_TZSET */
  45. struct timeb {
  46.     time_t        time;        /* Seconds since the epoch    */
  47.     unsigned short    millitm;    /* Field not used        */
  48.     short        timezone;
  49.     short        dstflag;    /* Field not used        */
  50. };
  51. #else
  52. #include <sys/timeb.h>
  53. #endif    /* defined(USG) */
  54. #if    defined(BSD4_2) || defined(BSD4_1C)
  55. #include <sys/time.h>
  56. #else
  57. #include <time.h>
  58. #endif    /* defined(BSD4_2) */
  59. #endif    /* defined(vms) */
  60.  
  61. #if defined (STDC_HEADERS) || defined (USG)
  62. #include <string.h>
  63. #endif
  64.  
  65. extern struct tm    *localtime();
  66.  
  67. #define yyparse getdate_yyparse
  68. #define yylex getdate_yylex
  69. #define yyerror getdate_yyerror
  70.  
  71. #if    !defined(lint) && !defined(SABER)
  72. static char RCS[] =
  73.     "$Header: str2date.y,v 2.1 90/09/06 08:15:06 cronan Exp $";
  74. #endif    /* !defined(lint) && !defined(SABER) */
  75.  
  76.  
  77. #define EPOCH        1970
  78. #define HOUR(x)        (x * 60)
  79. #define SECSPERDAY    (24L * 60L * 60L)
  80.  
  81.  
  82. /*
  83. **  An entry in the lexical lookup table.
  84. */
  85. typedef struct _TABLE {
  86.     char    *name;
  87.     int        type;
  88.     time_t    value;
  89. } TABLE;
  90.  
  91.  
  92. /*
  93. **  Daylight-savings mode:  on, off, or not yet known.
  94. */
  95. typedef enum _DSTMODE {
  96.     DSTon, DSToff, DSTmaybe
  97. } DSTMODE;
  98.  
  99. /*
  100. **  Meridian:  am, pm, or 24-hour style.
  101. */
  102. typedef enum _MERIDIAN {
  103.     MERam, MERpm, MER24
  104. } MERIDIAN;
  105.  
  106.  
  107. /*
  108. **  Global variables.  We could get rid of most of these by using a good
  109. **  union as the yacc stack.  (This routine was originally written before
  110. **  yacc had the %union construct.)  Maybe someday; right now we only use
  111. **  the %union very rarely.
  112. */
  113. static char    *yyInput;
  114. static DSTMODE    yyDSTmode;
  115. static time_t    yyDayOrdinal;
  116. static time_t    yyDayNumber;
  117. static int    yyHaveDate;
  118. static int    yyHaveDay;
  119. static int    yyHaveRel;
  120. static int    yyHaveTime;
  121. static int    yyHaveZone;
  122. static time_t    yyTimezone;
  123. static time_t    yyDay;
  124. static time_t    yyHour;
  125. static time_t    yyMinutes;
  126. static time_t    yyMonth;
  127. static time_t    yySeconds;
  128. static time_t    yyYear;
  129. static MERIDIAN    yyMeridian;
  130. static time_t    yyRelMonth;
  131. static time_t    yyRelSeconds;
  132.  
  133. %}
  134.  
  135. %union {
  136.     time_t        Number;
  137.     enum _MERIDIAN    Meridian;
  138. }
  139.  
  140. %token    tAGO tDAY tDAYZONE tID tMERIDIAN tMINUTE_UNIT tMONTH tMONTH_UNIT
  141. %token    tSEC_UNIT tSNUMBER tUNUMBER tZONE
  142.  
  143. %type    <Number>    tDAY tDAYZONE tMINUTE_UNIT tMONTH tMONTH_UNIT
  144. %type    <Number>    tSEC_UNIT tSNUMBER tUNUMBER tZONE
  145. %type    <Meridian>    tMERIDIAN o_merid
  146.  
  147. %%
  148.  
  149. spec    : /* NULL */
  150.     | spec item
  151.     ;
  152.  
  153. item    : time {
  154.         yyHaveTime++;
  155.     }
  156.     | zone {
  157.         yyHaveZone++;
  158.     }
  159.     | date {
  160.         yyHaveDate++;
  161.     }
  162.     | day {
  163.         yyHaveDay++;
  164.     }
  165.     | rel {
  166.         yyHaveRel++;
  167.     }
  168.     | number
  169.     ;
  170.  
  171. time    : tUNUMBER tMERIDIAN {
  172.         yyHour = $1;
  173.         yyMinutes = 0;
  174.         yySeconds = 0;
  175.         yyMeridian = $2;
  176.     }
  177.     | tUNUMBER ':' tUNUMBER o_merid {
  178.         yyHour = $1;
  179.         yyMinutes = $3;
  180.         yySeconds = 0;
  181.         yyMeridian = $4;
  182.     }
  183.     | tUNUMBER ':' tUNUMBER tSNUMBER {
  184.         yyHour = $1;
  185.         yyMinutes = $3;
  186.         yyMeridian = MER24;
  187.         yyDSTmode = DSToff;
  188.         yyTimezone = - ($4 % 100 + ($4 / 100) * 60);
  189.     }
  190.     | tUNUMBER ':' tUNUMBER ':' tUNUMBER o_merid {
  191.         yyHour = $1;
  192.         yyMinutes = $3;
  193.         yySeconds = $5;
  194.         yyMeridian = $6;
  195.     }
  196.     | tUNUMBER ':' tUNUMBER ':' tUNUMBER tSNUMBER {
  197.         yyHour = $1;
  198.         yyMinutes = $3;
  199.         yySeconds = $5;
  200.         yyMeridian = MER24;
  201.         yyDSTmode = DSToff;
  202.         yyTimezone = - ($6 % 100 + ($6 / 100) * 60);
  203.     }
  204.     ;
  205.  
  206. zone    : tZONE {
  207.         yyTimezone = $1;
  208.         yyDSTmode = DSToff;
  209.     }
  210.     | tDAYZONE {
  211.         yyTimezone = $1;
  212.         yyDSTmode = DSTon;
  213.     }
  214.     ;
  215.  
  216. day    : tDAY {
  217.         yyDayOrdinal = 1;
  218.         yyDayNumber = $1;
  219.     }
  220.     | tDAY ',' {
  221.         yyDayOrdinal = 1;
  222.         yyDayNumber = $1;
  223.     }
  224.     | tUNUMBER tDAY {
  225.         yyDayOrdinal = $1;
  226.         yyDayNumber = $2;
  227.     }
  228.     ;
  229.  
  230. date    : tUNUMBER '/' tUNUMBER {
  231.         yyMonth = $1;
  232.         yyDay = $3;
  233.     }
  234.     | tUNUMBER '/' tUNUMBER '/' tUNUMBER {
  235.         yyMonth = $1;
  236.         yyDay = $3;
  237.         yyYear = $5;
  238.     }
  239.     | tMONTH tUNUMBER {
  240.         yyMonth = $1;
  241.         yyDay = $2;
  242.     }
  243.     | tMONTH tUNUMBER ',' tUNUMBER {
  244.         yyMonth = $1;
  245.         yyDay = $2;
  246.         yyYear = $4;
  247.     }
  248.     | tUNUMBER tMONTH {
  249.         yyMonth = $2;
  250.         yyDay = $1;
  251.     }
  252.     | tUNUMBER tMONTH tUNUMBER {
  253.         yyMonth = $2;
  254.         yyDay = $1;
  255.         yyYear = $3;
  256.     }
  257.     ;
  258.  
  259. rel    : relunit tAGO {
  260.         yyRelSeconds = -yyRelSeconds;
  261.         yyRelMonth = -yyRelMonth;
  262.     }
  263.     | relunit
  264.     ;
  265.  
  266. relunit    : tUNUMBER tMINUTE_UNIT {
  267.         yyRelSeconds += $1 * $2 * 60L;
  268.     }
  269.     | tSNUMBER tMINUTE_UNIT {
  270.         yyRelSeconds += $1 * $2 * 60L;
  271.     }
  272.     | tMINUTE_UNIT {
  273.         yyRelSeconds += $1 * 60L;
  274.     }
  275.     | tSNUMBER tSEC_UNIT {
  276.         yyRelSeconds += $1;
  277.     }
  278.     | tUNUMBER tSEC_UNIT {
  279.         yyRelSeconds += $1;
  280.     }
  281.     | tSEC_UNIT {
  282.         yyRelSeconds++;
  283.     }
  284.     | tSNUMBER tMONTH_UNIT {
  285.         yyRelMonth += $1 * $2;
  286.     }
  287.     | tUNUMBER tMONTH_UNIT {
  288.         yyRelMonth += $1 * $2;
  289.     }
  290.     | tMONTH_UNIT {
  291.         yyRelMonth += $1;
  292.     }
  293.     ;
  294.  
  295. number    : tUNUMBER {
  296.         if (yyHaveTime && yyHaveDate && !yyHaveRel)
  297.         yyYear = $1;
  298.         else {
  299.         if($1>10000) {
  300.             time_t date_part;
  301.  
  302.             date_part= $1/10000;
  303.             yyHaveDate++;
  304.             yyDay= (date_part)%100;
  305.             yyMonth= (date_part/100)%100;
  306.             yyYear = date_part/10000;
  307.         } 
  308.             yyHaveTime++;
  309.         if ($1 < 100) {
  310.             yyHour = $1;
  311.             yyMinutes = 0;
  312.         }
  313.         else {
  314.             yyHour = $1 / 100;
  315.             yyMinutes = $1 % 100;
  316.         }
  317.         yySeconds = 0;
  318.         yyMeridian = MER24;
  319.         }
  320.     }
  321.     ;
  322.  
  323. o_merid    : /* NULL */ {
  324.         $$ = MER24;
  325.     }
  326.     | tMERIDIAN {
  327.         $$ = $1;
  328.     }
  329.     ;
  330.  
  331. %%
  332.  
  333. /* Month and day table. */
  334. static TABLE    MonthDayTable[] = {
  335.     { "january",    tMONTH,  1 },
  336.     { "february",    tMONTH,  2 },
  337.     { "march",        tMONTH,  3 },
  338.     { "april",        tMONTH,  4 },
  339.     { "may",        tMONTH,  5 },
  340.     { "june",        tMONTH,  6 },
  341.     { "july",        tMONTH,  7 },
  342.     { "august",        tMONTH,  8 },
  343.     { "september",    tMONTH,  9 },
  344.     { "sept",        tMONTH,  9 },
  345.     { "october",    tMONTH, 10 },
  346.     { "november",    tMONTH, 11 },
  347.     { "december",    tMONTH, 12 },
  348.     { "sunday",        tDAY, 0 },
  349.     { "monday",        tDAY, 1 },
  350.     { "tuesday",    tDAY, 2 },
  351.     { "tues",        tDAY, 2 },
  352.     { "wednesday",    tDAY, 3 },
  353.     { "wednes",        tDAY, 3 },
  354.     { "thursday",    tDAY, 4 },
  355.     { "thur",        tDAY, 4 },
  356.     { "thurs",        tDAY, 4 },
  357.     { "friday",        tDAY, 5 },
  358.     { "saturday",    tDAY, 6 },
  359.     { NULL }
  360. };
  361.  
  362. /* Time units table. */
  363. static TABLE    UnitsTable[] = {
  364.     { "year",        tMONTH_UNIT,    12 },
  365.     { "month",        tMONTH_UNIT,    1 },
  366.     { "fortnight",    tMINUTE_UNIT,    14 * 24 * 60 },
  367.     { "week",        tMINUTE_UNIT,    7 * 24 * 60 },
  368.     { "day",        tMINUTE_UNIT,    1 * 24 * 60 },
  369.     { "hour",        tMINUTE_UNIT,    60 },
  370.     { "minute",        tMINUTE_UNIT,    1 },
  371.     { "min",        tMINUTE_UNIT,    1 },
  372.     { "second",        tSEC_UNIT,    1 },
  373.     { "sec",        tSEC_UNIT,    1 },
  374.     { NULL }
  375. };
  376.  
  377. /* Assorted relative-time words. */
  378. static TABLE    OtherTable[] = {
  379.     { "tomorrow",    tMINUTE_UNIT,    1 * 24 * 60 },
  380.     { "yesterday",    tMINUTE_UNIT,    -1 * 24 * 60 },
  381.     { "today",        tMINUTE_UNIT,    0 },
  382.     { "now",        tMINUTE_UNIT,    0 },
  383.     { "last",        tUNUMBER,    -1 },
  384.     { "this",        tMINUTE_UNIT,    0 },
  385.     { "next",        tUNUMBER,    2 },
  386.     { "fi